# MAPLE ASSIGNMENT 10
# CONSTRAINED OPTIMIZATION
# The first part of this assignment illustrates the example discussed in
# class. The constant c has been 
# put equal to 10 but you can change that if you like.
> f:=3*x*y+4*y^2;
> plot3d(f,x=-5..5,y=-5..5);
> g:=x^2+y^2;
# Below z is used for the Lagrange multiplier.
> L:=f+z*(10-g);
> with(linalg):
> gradL:=grad(L,[x,y,z]);
> extpt:=solve({gradL[1],gradL[2],gradL[3]},{x,y,z});
# These are the 4 extreme points of the problem. We also need the value
# of f at each extreme point.
> f1:=subs(extpt[1],L);
> f2:=subs(extpt[2],L);
> f3:=subs(extpt[3],L);
> f4:=subs(extpt[4],L);
> with(plots):
# The next plot shows the level curves of f being tangent to the
# constraint curve at the local maxs and 
# mins of the constrained optimization problem.  If you click on the
# plot and then on the constrained option in the projection menu, the
# circular constraint will be made into an exact circle. 
> implicitplot({f=f1,f=f3,g=10},x=-5..5,y=-5..5);
> hessian(L,[z,x,y]);
> mulrow(",1,-1);
> B:=mulcol(",1,-1);
# The row and column scaler multiplications were not really necessary as
# the first matrix above could be used in the same way as the bordered
# Hessian. Now check the SOC first at extreme point 1, then 
# edit the commands to check at extpt[3]. Which point is the local max?
# the local min? You already know the answer from calculating the values
# of f at the extreme points above.
> dB:=det(B);
> subs(extpt[3],dB);
# A CONSUMER MAXIMIZING UTILITY
# The first part of this exercise is to show that the U used here is a
# satisfactory utility function.
> U:=(sqrt(x)+sqrt(y))^2;

                                1/2    1/2 2
                         U := (x    + y   )

> plot3d(U(x,y),x=0..5,y=0..5);
> gradU:=grad(U,[x,y]);
> HU:=hessian(U,[x,y]);
> simplify(HU[1,1]);
> det(HU);
> S:=stack(gradU,HU);
> matrix(3,1,[0,gradU[1],gradU[2]]);
> HUb:=concat(",S);
> det(HUb);
# This utility function has positive diminishing marginal utilities and
# is concave but not strictly concave. The indifference curves are
# strictly convex because MUx  > 0 and U is strictly quasiconcave. Now
# you can solve for the consumer's demand functions.
> L:=U+z*(m-p*x-q*y);

                      1/2    1/2 2
               L := (x    + y   )  + z (m - p x - q y)

> with(linalg):
Warning, new definition for norm
Warning, new definition for trace
> gradL:=grad(L,[x,y,z]);

             [ 1/2    1/2         1/2    1/2                     ]
             [x    + y           x    + y                        ]
    gradL := [----------- - z p, ----------- - z q, m - p x - q y]
             [    1/2                1/2                         ]
             [   x                  y                            ]

> dem:=solve({gradL[1],gradL[2],gradL[3]},{x,y,z});

                     m          m
dem := {z = 0, x = -----, y = -----},
                   p + q      p + q

         p + q         m q            m p
    {z = -----, x = ---------, y = ---------}
          q p       p (p + q)      q (p + q)

# The solution with z>0 is the two demand functions of this consumer.
# The other solution can easily be 
# seen not to be a solution of the FOC so is extraneous (erroneously
# produced by Maple's solution 
# procedure). Now you can check the SOC for maximization. However, this
# is guaranteed to be true because of the properties of U.
> B:=hessian(L,[z,x,y]);
> mulrow(B,1,-1);
> mulcol(",1,-1);
> det(");
# To illustrate the consumer's optimal choices in a plot, we apparently
# need them as functions so this conversion is made below  The correct
# answer is mq/p/(p+q). If you don't get this try the other component of
# dem.
> xd:=subs(dem[2],x);
> xdem:=unapply(xd,p,q,m);
# In the following, the demand for x is plotted against its own price,
# then the demand curve is shifted 
# with income and with changes in the price of y (which way?).  Then the
# demand surface is plotted for a fixed income level. Does the demand
# surface conform with your expectations?
> plot(xdem(p,2,15),p=0..5,x=0..50);
> plot([xdem(p,2,15),xdem(p,2,30),xdem(p,2,45)],p=0..5,x=0..50);
> plot({xdem(p,2,15),xdem(p,8,15),xdem(p,26,15)},p=0..5,x=0..50);
> plot3d(xdem(p,q,15),p=0.1..5,q=0.1..5);
#  
# Now calculate the maximum utility level (indirect utility function).
# Be careful that you 
# substitute the right component of dem into L.
# 
> V:=subs(dem[2],L);

                                                  /     m q     m p \
                                          (p + q) |m - ----- - -----|
     //   m q   \1/2   /   m p   \1/2\2           \    p + q   p + q/
V := ||---------|    + |---------|   |  + ---------------------------
     \\p (p + q)/      \q (p + q)/   /                q p

> V:=simplify(");

                    //   m q   \1/2   /   m p   \1/2\2
               V := ||---------|    + |---------|   |
                    \\p (p + q)/      \q (p + q)/   /

# The indirect utility function can be used to help plot the
# indifference curve and the budget line corresponding to particular
# values for particular values of p, q and m. Then calculate the actual
# demands and see if clicking on the tangency point gives similar
# answers.
> u1:=subs({p=3,q=2,m=15},V);

                            1/2        1/2  1/2 2
                    u1 := (2    + 1/2 9    2   )

> with(plots):
> implicitplot({U(x,y)=u1,3*x+2*y=15},x=0..10,y=0..10);
> dem1:=subs({p=3,q=2,m=15},dem[2]);

                  dem1 := {y = 9/2, z = 5/6, x = 2}

# Next change p to 2.5 (or anything else you like) and see how demand
# changes.
> dem2:=subs({p=2.5,q=2,m=15},dem[2]);

     dem2 := {x = 2.666666667, z = .9000000000, y = 4.166666667}

# The demand for x increases,  the demand for y falls and welfare
# increases.
> u2:=subs({p=2.5,q=2,m=15},V);

                          u2 := 13.50000000

> evalf(u2-u1);

                              1.00000001

> implicitplot({U(x,y)=u1,3*x+2*y=15,U(x,y)=u2,2.5*x+2*y=15},x=0..10,y=0
> ..10);
#  
#  To determine the compensation and income effect, one needs to know
# the minimum income
# needed to reach a given level of utility. The next command derives the
# formula.
> expd:=solve(V=u,m);

                            u p q   u q p (p + q)
                    expd := -----, ---------------
                            p + q   2    2
                                   p  + q  - 2 p q

# One solution here is extraneous. You can check below which solution 
# is correct. Change the ? to 1 or 2. Then let p fall to 2.5 and
# calculate the reduced income that would keep this
# consumer on the same indifference curve (ame level of utilty) as when
# p = 3.
> m1:=subs({p=3,q=2,u=u1},expd[?]);

                              1/2        1/2  1/2 2
                  m1 := 6/5 (2    + 1/2 9    2   )

> simplify(m1);

                                  15

> m2:=subs({p=2.5,q=2,u=u1},expd[?]);

                                  1/2        1/2  1/2 2
              m2 := 1.111111111 (2    + 1/2 9    2   )

> simplify(m2);

                             13.88888889

# The compensation effect is the difference between the demands with p
# =3, q =2 and m =15
# and the demands with p = 2.5, q =2 and m = m2. 
> dem3:=evalf(subs({p=2.5,q=2,m=m2},dem[2]));

     dem3 := {y = 3.858024688, x = 2.469135801, z = .9000000000}

> evalf(subs(dem3,U)-u1);

                                  0

> implicitplot({U(x,y)=u1,3*x+2*y=15,2.5*x+2*y=m2},x=0..10,y=0..10);

# Due to the fall in the price of x, the demand for x increases and the
# demand for y falls and the level of utility stays constant. The
# difference between dem3 and dem2 is the income effect
> dem3;

         {y = 3.858024688, x = 2.469135801, z = .9000000000}

> dem2;

         {x = 2.666666667, z = .9000000000, y = 4.166666667}

# The income effect increases the demand for both goods. 
# But overall the demand for y falls because the compensation
# effect is larger in magnitude than the income effect. 
> dem1;

                      {y = 9/2, z = 5/6, x = 2}

# In the last plot both effects are illustrated,  The scale of x and y
# has been reduced to make the
# effects look larger.
> implicitplot({U(x,y)=u1,U(x,y)=u2,3*x+2*y=15,2.5*x+2*y=15,2.5*x+2*y=m2
> },x=0..5,y=3..6);

# dem1 is the intersection of the lower indifference curve with the
# budget line whose x intercept is 3.
> 
